import {getDictsForRegister} from '@/api/system/dict/data'
import { getStudent } from '@/api/forum/student'
import {listCollege,regListCollege} from "@/api/forum/college"
import {listSchool,regListSchool} from "@/api/forum/school"
import {listSpecialty,regListSpecialty} from "@/api/forum/specialty"
import {defineStore} from 'pinia'

const useBaseStore = defineStore(
  'base',
  {
    state: () => ({
        studentList:[],
        specialtyList: [],
        filterSpecialtyList: null,
        collegeList: [],
        filterCollegeList: null,
        schoolList: null,
        filterSchoolList: null,
        isLoadCollege: false,
        isLoadSchool: false,
        isLoadSpecialty: false,
        schoolId: null,
        collegeId: null,
        specialtyId: null,
        gradeList: [],
    }),
    actions: {
        //获取年级信息
        getGradeList(){
            var dictName = 'forum_grade'
            getDictsForRegister(dictName).then(res=>{
                if(dictName == 'forum_grade'){
                  this.gradeList = res.data;
                }
            })
        },
        //初始化筛选
        createFilter(schoolId,collegeId){
            // console.log(this.schoolList);
            // console.log(this.collegeList);
            // console.log(this.specialtyList);
            this.filterSchoolList = this.schoolList;
            this.filterCollegeListForSchoolId(schoolId);
            this.filterSpecialtyListForCollegeId(collegeId);
        },
        //选择学校
        changeSchool(schoolId){
            this.filterCollegeListForSchoolId(schoolId);
        },
        //选择学院
        changeCollege(collegeId){
            this.filterSpecialtyListForCollegeId(collegeId);
        },
        filterCollegeListForSchoolId(schoolId){
            var tempList = [];
            for(var i=0;i<this.collegeList.length;i++){
                // console.log('schoolId',schoolId)
                // console.log('this.collegeList[i].id',this.collegeList[i].id)
                if(schoolId==this.collegeList[i].schoolId){
                    tempList.push(this.collegeList[i]);
                }
            }
            this.filterCollegeList = tempList;
        },
        filterSpecialtyListForCollegeId(collegeId){
            var tempList = [];
            for(var i=0;i<this.specialtyList.length;i++){
                // console.log('collegeId',collegeId)
                // console.log('this.specialtyList[i].id',this.specialtyList[i].id)
                if(collegeId==this.specialtyList[i].collegeId){
                    tempList.push(this.specialtyList[i]);
                }
            }
            this.filterSpecialtyList = tempList;
        },
        //获取学校列表
        getSchoolList(){
            if(this.isLoadSchool){
                return ;
            }
            return new Promise((resolve, reject) => {
                regListSchool().then(res => {
                  this.schoolList = res.rows;
                  this.isLoadSchool = true;
                  resolve()
                }).catch(error => {
                  reject(error)
                })
              })
        },
        //获取学校根据id
        getSchoolForId(schoolId){
            var school =null;
            if(this.isLoadSchool && schoolId!=null){
                for(var i=0 ;i<this.schoolList.length;i++){
                    if(schoolId == this.schoolList[i].id){
                        school = this.schoolList[i];
                    }
                }
            }else{
                this.getSchoolList();
            }
            // console.log('school',school)
            return school;
        },
        //获取学院列表
        getCollegeList(){
            if(this.isLoadCollege){
                return ;
            }
            this.isLoadCollege = false;
            // console.log("开始获取学院数据。")
            return new Promise((resolve, reject) => {
                regListCollege().then(res => {
                  this.collegeList = res.rows;
                  this.isLoadCollege = true;
                  resolve()
                }).catch(error => {
                  this.isLoadCollege = false;
                  reject(error)
                })
              })
        },
        //获取学院根据id
        getCollegeForId(collegeId){
            var college =null;
            if(this.isLoadCollege && collegeId!=null){
                for(var i=0 ;i<this.collegeList.length;i++){
                    if(collegeId == this.collegeList[i].id){
                        college = this.collegeList[i];
                    }
                }
            }else{
                this.getCollegeList();
            }
            // console.log('college',college)
            return college;
        },
        //获取专业列表
        getSpecialtyList(){
            if(this.isLoadSchool){
                return ;
            }
            return new Promise((resolve, reject) => {
                regListSpecialty().then(res => {
                  this.specialtyList = res.rows;
                  this.isLoadSpecialty = true;
                  resolve()
                }).catch(error => {
                  reject(error)
                })
              })
        },
        //获取专业根据id
        getSpecialtyForId(specialtyId){
            var specialty =null;
            if(this.isLoadSpecialty && specialtyId!=null){
                for(var i=0 ;i<this.specialtyList.length;i++){
                    if(specialtyId == this.specialtyList[i].id){
                        specialty = this.specialtyList[i];
                    }
                }
            }else{
                this.getSpecialtyList();
            }
            // console.log('specialty',specialty)
            return specialty;
        },
        //获取单个学生信息
        getStudentForId(studentId){
            if(studentId==null){
                return null;
            }
            var student = null
            for(var i=0;i<this.studentList.length;i++){
                if(studentId == this.studentList[i].studentId){
                    console.log("在")
                    student = this.studentList[i];
                }
            }
            if(student!=null){
                console.log("student！=null")
                return student;
            }else{
                console.log("student==null")
                getStudent(studentId).then(res => {
                  this.studentList.push(res.data);
                  return res.data;
                })
            }
            
        },
    }
  })

export default useBaseStore